<?php

namespace app\common\service\driver;

use app\common\model\driving\Driver;
use app\common\model\driving\DriverInvite;
use think\Db;
use think\Exception;

/**
 * 司机邀请服务类
 **/
class DriverInviteService
{

    /**
     * 我的小队
     *
     * @param mixed $user 用户信息
     * @param string $function
     * @return array
     */
    public static function index($user,$function)
    {
        $list = DriverInvite::where('parent_id',$user['id'])
            ->field('parent_id',true)
            ->order('id desc')
            ->select();
        $total_num = count($list);
        $today_num = 0;
        $total_amount = 0;
        $today_amount = 0;
        $mk_time = mk_time(1,[]);
        $start = date('Y-m-d H:i:s',$mk_time['start']);
        $end = date('Y-m-d H:i:s',$mk_time['end']);
        $ids = [];
        foreach ($list as $val){
            $total_amount = bcadd($total_amount,$val['money'],2);
            if($val['createtime'] >= $start && $val['createtime'] <= $end){
                $today_num++;
                $today_amount = bcadd($today_amount,$val['money'],2);
            }
            if($function == 'list'){
                $ids[] = $val['driver_id'];
            }
        }
        if($function == 'index'){
            return compact('total_num','today_num','total_amount','today_amount');
        }
        if($function == 'list'){
            return compact('total_num','today_num','total_amount','today_amount','ids');
        }
        if($function == 'list_money'){
            $data = $list;
            return compact('total_num','today_num','total_amount','today_amount','data');
        }
    }

    /**
     * 列表
     *
     * @param mixed $user 用户信息
     * @param string $function
     * @return array
     */
    public static function list($user, $function)
    {
        $index = self::index($user, $function);
        if($function == 'list_money'){
           return $index;
        }
        if($function == 'list'){
            $where = [];
            $where['parent_id'] = $user['id'];
            $list = Driver::where('id','in',$index['ids'])->field('id,nickname,avatar,FROM_UNIXTIME(createtime,"%Y-%m-%d") as createtime')->order('id desc')->select();
            unset($index['ids']);
            return array_merge($index,['data'=>$list]);
        }
    }

}
